
/*  BEGIN_FILE_HDR
******************************************Copyright(C)*****************************************
*
*                                       YKXH  Technology
*
***********************************File Info***************************************************
*   File Name       @: OsekNm_Cfg.h
************************************************************************************************
*   Project/Product @:
*   Title           @:
*   Author          @: donglin.Zhang
************************************************************************************************
*   Description     @:
*
************************************************************************************************
*   Limitations     @: None
*
************************************************************************************************
*
************************************************************************************************
*   Revision History:
*
*    Version    Date          Initials       CR#             Descriptions
*   ---------   ----------    -------------  ----------    ---------------
*   1.0         2018/05/30    donglin.zhang   N/A          Original
*   1.1         xxxx/xx/xx    xxxxx           N/A          xxxxxxxx
*
************************************************************************************************
* END_FILE_HDR*/

#ifndef _OSEKNM_CFG_H_
#define _OSEKNM_CFG_H_

/* Includes */
/**********************************************************************************************/
#include "../src/BSW/Comms/NM/CanIf/CanIf.h"
/************************************ Program Body ********************************************/
//#pragma MESSAGE DISABLE C166 //WARNING C1106: Non-standard bitfield type */

#define NM_BYTEORDER_LSB              	/* NM Order Type: LSB or MSB */

#define NM_NUMBER_OF_CHANNELS				0x1u	/*Multiply channel NetWork Management*/
#define NM_NUMBER_OF_CONFIG_NODES_MAX		0x7F
#define NM_NUMBER_OF_CONFIG_NODES		  	((NM_NUMBER_OF_CONFIG_NODES_MAX/8) + 1)

#define NM_MAINFUNCTION_PERIOD  10

#define NM_EXTEND_CAN_ID_MASK				0x000000ff
#define NmExtendIdToStand(ExtendedCanId)	((ExtendedCanId & NM_EXTEND_CAN_ID_MASK)+ NM_PDU_BASICID )

#define NM_CAN_TXID			  				(NM_PDU_BASICID + NM_OWN_STATION)
#define nmRxMsgBasicID						(uint8)(nmRxMsgId & NM_PDU_BASICID_MASK)
#define nmRxMsgSourceID						(uint8)(nmRxMsgId & NM_PDU_SOUCEID_MASK)

#define CAN_MSGBUFFER_CLRLESS				STD_ON	//indicate whether the Can Controller Buffer can be cleared

#define T_ACTIVE_MINI_ON					STD_ON
#define T_LIMPHOME_MINI_ON					STD_ON
#define NORWBSCANCEL_INDCLR					STD_ON
#define LHTWBSCANCEL_INDCLR					STD_ON
#define LH_PREPSLEEP_INDCLR					STD_ON
#define LEAVE_WBS_INDORACK_CLR				STD_ON

#if(NM_NUMBER_OF_CHANNELS >= 1)
	#define CN0_ID_STANDARD					STD_ON
	#define CN0_REMOTE_WAKEUP				STD_ON
	#define CN0_USER_DATA					STD_ON
	#define CN0_NETWORK_CONFIG				STD_ON
	#define CN0_EXT_CALLBACK				STD_ON
#endif

#if(NM_NUMBER_OF_CHANNELS >= 1)
	#if defined ( NM_BYTEORDER_LSB )
	#define CN0_CONFIGURATION		((uint8)CN0_ID_STANDARD 	<<0)|\
									((uint8)CN0_REMOTE_WAKEUP	<<1)|\
									((uint8)CN0_USER_DATA		<<2)|\
									((uint8)CN0_NETWORK_CONFIG	<<3)|\
									((uint8)CN0_EXT_CALLBACK	<<4)
	#else
	#define CN0_CONFIGURATION		((uint8)CN0_ID_STANDARD 	<<7)|\
									((uint8)CN0_REMOTE_WAKEUP	<<6)|\
									((uint8)CN0_USER_DATA		<<5)|\
									((uint8)CN0_NETWORK_CONFIG	<<4)|\
									((uint8)CN0_EXT_CALLBACK	<<3)
	#endif
#endif

#if(NM_NUMBER_OF_CHANNELS >= 2)
	#if defined ( NM_BYTEORDER_LSB )
	#define CN1_CONFIGURATION		((uint8)CN1_ID_STANDARD 	<<0)|\
									((uint8)CN1_REMOTE_WAKEUP	<<1)|\
									((uint8)CN1_USER_DATA		<<2)|\
									((uint8)CN1_NETWORK_CONFIG	<<3)|\
									((uint8)CN1_EXT_CALLBACK	<<4)
	#else
	#define CN1_CONFIGURATION		((uint8)CN1_ID_STANDARD 	<<7)|\
									((uint8)CN1_REMOTE_WAKEUP	<<6)|\
									((uint8)CN1_USER_DATA		<<5)|\
									((uint8)CN1_NETWORK_CONFIG	<<4)|\
									((uint8)CN1_EXT_CALLBACK	<<3)
	#endif
#endif

#if(NM_NUMBER_OF_CHANNELS >= 3)
	#if defined ( NM_BYTEORDER_LSB )
	#define CN2_CONFIGURATION		((uint8)CN2_ID_STANDARD 	<<0)|\
									((uint8)CN2_REMOTE_WAKEUP	<<1)|\
									((uint8)CN2_USER_DATA		<<2)|\
									((uint8)CN2_NETWORK_CONFIG	<<3)|\
									((uint8)CN2_EXT_CALLBACK	<<4)
	#else
	#define CN2_CONFIGURATION		((uint8)CN2_ID_STANDARD 	<<7)|\
									((uint8)CN2_REMOTE_WAKEUP	<<6)|\
									((uint8)CN2_USER_DATA		<<5)|\
									((uint8)CN2_NETWORK_CONFIG	<<4)|\
									((uint8)CN2_EXT_CALLBACK	<<3)
	#endif
#endif

#if(NM_NUMBER_OF_CHANNELS >= 4)
	#if defined ( NM_BYTEORDER_LSB )
	#define CN3_CONFIGURATION		((uint8)CN3_ID_STANDARD 	<<0)|\
									((uint8)CN3_REMOTE_WAKEUP	<<1)|\
									((uint8)CN3_USER_DATA		<<2)|\
									((uint8)CN3_NETWORK_CONFIG	<<3)|\
									((uint8)CN3_EXT_CALLBACK	<<4)
	#else
	#define CN3_CONFIGURATION		((uint8)CN3_ID_STANDARD 	<<7)|\
									((uint8)CN3_REMOTE_WAKEUP	<<6)|\
									((uint8)CN3_USER_DATA		<<5)|\
									((uint8)CN3_NETWORK_CONFIG	<<4)|\
									((uint8)CN3_EXT_CALLBACK	<<3)
	#endif
#endif

#if(NM_NUMBER_OF_CHANNELS >= 5)
	#if defined ( NM_BYTEORDER_LSB )
	#define CN4_CONFIGURATION		((uint8)CN4_ID_STANDARD 	<<0)|\
									((uint8)CN4_REMOTE_WAKEUP	<<1)|\
									((uint8)CN4_USER_DATA		<<2)|\
									((uint8)CN4_NETWORK_CONFIG	<<3)|\
									((uint8)CN4_EXT_CALLBACK	<<4)
	#else
	#define CN4_CONFIGURATION		((uint8)CN4_ID_STANDARD 	<<7)|\
									((uint8)CN4_REMOTE_WAKEUP	<<6)|\
									((uint8)CN4_USER_DATA		<<5)|\
									((uint8)CN4_NETWORK_CONFIG	<<4)|\
									((uint8)CN4_EXT_CALLBACK	<<3)
	#endif
#endif

typedef union
{
	uint8 R;
	struct
	{
#if defined ( NM_BYTEORDER_LSB )
		uint8    NMIDStandardEn     :1;     /* byte0 bit0 */
		uint8    NMRemoteWakeUpEn   :1;     /* byte0 bit1 */
		uint8    NMUseringDataEn    :1;     /* byte0 bit2 */
		uint8    NMConfigEn        	:1;     /* byte0 bit3 */
		uint8    NMExtCallBackEn    :1;     /* byte0 bit4 */
		uint8    NMReserved0        :1;     /* byte0 bit5 */
		uint8    NMReserved1        :1;     /* byte0 bit6 */
		uint8    NMReserved2       	:1;     /* byte0 bit7 */
#else
		uint8    NMReserved2       	:1;     /* byte0 bit0 */
		uint8    NMReserved1        :1;     /* byte0 bit1 */
		uint8    NMReserved0        :1;     /* byte0 bit2 */
		uint8    NMExtCallBackEn    :1;     /* byte0 bit3 */
		uint8    NMConfigEn        	:1;     /* byte0 bit4 */
		uint8    NMUseringDataEn    :1;     /* byte0 bit5 */
		uint8    NMRemoteWakeUpEn   :1;     /* byte0 bit6 */
		uint8    NMIDStandardEn     :1;     /* byte0 bit7 */
#endif
	}B;
} nmChannelConfigUnion;

/*Description: This type represents the various status flags.*/
typedef union
{
	uint8 R[3];
	struct
	{
#if defined ( NM_BYTEORDER_LSB )
		uint8    NMLimpHome        :1;     /* byte0 bit0 */
		uint8    NMPrepSleep       :1;     /* byte0 bit1 */
		uint8    NMTwbs            :1;     /* byte0 bit2 */
		uint8    NMBusSleep        :1;     /* byte0 bit3 */
		uint8    NMOff             :1;     /* byte0 bit4 */
		uint8    NMActive          :1;     /* byte0 bit5 */
		uint8    NMBusError        :1;     /* byte0 bit6 */
		uint8    NMCfgStable       :1;     /* byte0 bit7 */

		uint8    TransmitFlag      :1;     /* byte1 bit0 */
		uint8    UseRingData       :1;     /* byte1 bit1 */   /*unusable*/
		uint8    ImDestined        :1;     /* byte1 bit2 */   /*unusable*/
		uint8    ImSkipped         :1;     /* byte1 bit3 */
		uint8    MerkerLimpHome    :1;     /* byte1 bit4 */
		uint8    MerkerStable      :1;     /* byte1 bit5 */
		uint8    LocalSleepAck     :1;     /* byte1 bit6 */   /*unusable*/
		uint8    LocalSleepInd     :1;     /* byte1 bit7 */

		uint8    ActTimeReach      :1;     /* byte1 bit0 */ /*Indicate whether T[ActiveMini] has reached*/
		uint8    AliveSended       :1;     /* byte1 bit1 */ /*Indicate whether The first Alive message has sent after wake up*/
		uint8    LHTimeReach       :1;     /* byte1 bit2 */ /*Indicate whether T[LimpHome] has reached*/
		uint8    LHtoBusSleep      :1;     /* byte1 bit3 */ /*Indicate whether from state Normal to WaitBusSleep is directly*/
 	  	uint8    Reserved0	       :1;     /* byte1 bit4 */ /*Indicate whether from state LimpHome to WaitBusSleep is directly*/
 	 	uint8    Reserved1         :1;     /* byte1 bit5 */
 	 	uint8    Reserved2         :1;     /* byte1 bit6 */
 	  	uint8    Reserved3         :1;     /* byte1 bit6 */
#else
		uint8    NMCfgStable       :1;     /* byte0 bit0 */
		uint8    NMBusError        :1;     /* byte0 bit1 */
		uint8    NMActive          :1;     /* byte0 bit2 */
		uint8    NMOff             :1;     /* byte0 bit3 */
		uint8    NMBusSleep        :1;     /* byte0 bit4 */
		uint8    NMTwbs            :1;     /* byte0 bit5 */
		uint8    NMPrepSleep       :1;     /* byte0 bit6 */
		uint8    NMLimpHome        :1;     /* byte0 bit7 */

		uint8    LocalSleepInd     :1;     /* byte1 bit0 */
		uint8    LocalSleepAck     :1;     /* byte1 bit1 */
		uint8    MerkerStable      :1;     /* byte1 bit2 */
		uint8    MerkerLimpHome    :1;     /* byte1 bit3 */
		uint8    ImSkipped         :1;     /* byte1 bit4 */
		uint8    ImDestined        :1;     /* byte1 bit5 */
		uint8    UseRingData       :1;     /* byte1 bit6 */
		uint8    TransmitFlag      :1;     /* byte1 bit7 */

 	  	uint8    Reserved3         :1;     /* byte1 bit0 */
 	  	uint8    Reserved2         :1;     /* byte1 bit1 */
 	  	uint8    Reserved1         :1;     /* byte1 bit2 */
 	  	uint8    Reserved0	       :1;     /* byte1 bit3 */
 	  	uint8    LHtoBusSleep      :1;     /* byte1 bit4 */
 	  	uint8    LHTimeReach       :1;     /* byte1 bit5 */
		uint8    AliveSended       :1;     /* byte1 bit6 */
		uint8    ActTimeReach      :1;     /* byte1 bit7 */
#endif
	}B;
} nmStatusTypeUnion;

/*Description: This type represents the Data Field layout of the CAN message.*/
typedef union
{
  uint8 R[8];
  struct
  {
    uint8   DestID;              			/* byte0 */
#ifdef NM_BYTEORDER_LSB
	uint8 	OpCodeMsgType		:3;	  	/* byte1 bit0..bit2 */
	uint8 	unused1				:1;	    /* byte1 bit3 Reserved*/
	uint8 	OpCodeSleepInd		:1;		/* byte1 bit4 */
	uint8 	OpCodeSleepAck		:1;		/* byte1 bit5 */
	uint8 	OpCodeRes			:2;		/* byte1 bit6 and bit7 Reserved */
#else
	uint8	OpCodeRes			:2;		/* byte1 bit7 and bit6 Reserved */
	uint8	OpCodeSleepAck		:1;		/* byte1 bit5 */
	uint8	OpCodeSleepInd		:1;		/* byte1 bit4 */
	uint8	unused1				:1;		/* byte1 bit3 Reserved*/
	uint8	OpCodeMsgType		:3;		/* byte1 bit2..bit0 */
#endif
	uint8  	unused2				:8;		  /* byte2 */
	uint8  	unused3				:8;		  /* byte3 */
	uint8  	unused4				:8;		  /* byte4 */
	uint8  	unused5				:8;		  /* byte5 */
	uint8  	unused6				:8;		  /* byte6 */
	uint8  	unused7				:8;		  /* byte7 */

  }B;
} nmDataFldTypeUnion;


typedef uint8 nmNetIdType;
typedef uint8 nmRxTxCntType;
typedef uint32 nmTimerCntType;

/* Defines for NM message bits */
#define ALIVE					0x01u
#define RING					0x02u
#define LIMPHOME				0x04u
#define SLEEP_IND				0x10u
#define SLEEP_ACK				0x20u


#define ConvToCanChn(channelID)   NmToCanChnAry[channelID]
#define ConvToNmChn(CtrllerID)    CanToNmChnAry[CtrllerID]

#define NMCanInit()
#define NMCanTransmit()					CanIf_Transmit(NMTXPDUID,&NmTxPduInfo)
#define NmCanInterruptRestore()		  	;//CANIF_LEAVE_CRITICAL_SECTION()
#define NmCanInterruptDisable()	  		;//CANIF_ENTER_CRITICAL_SECTION()
#define NmCanTxOffline(ControllerId)		;//CanIf_SetPduMode(ConvToCanChn(ControllerId),CANIF_SET_TX_OFFLINE)
#define NmCanTxOnline(ControllerId)			;//CanIf_SetPduMode(ConvToCanChn(ControllerId),CANIF_SET_TX_ONLINE)
#define NmCanWakeUp(ControllerId)			;//CanIf_SetControllerMode(ConvToCanChn(ControllerId),CANIF_CS_STARTED)
#define NmCanSleep(ControllerId)			;//CanIf_SetControllerMode(ConvToCanChn(ControllerId),CANIF_CS_SLEEP)
#if (STD_ON == CANIF_PUBLIC_TX_BUFFERING)
#define NmCanClearBuffer(ControllerId)		;//CanIf_ClearTransmitBuffer(ConvToCanChn(ControllerId))
#else
#define NmCanClearBuffer(ControllerId)
#endif
#define NMBusOffRecovery(ControllerId)		Can_InitController((ConvToCanChn(ControllerId)),0); (void)Can_SetControllerMode(ConvToCanChn(ControllerId),CANIF_CS_STARTED)

/*constant configuration array*/
extern const nmChannelConfigUnion nmChannelConfigAry[NM_NUMBER_OF_CHANNELS];/*Configuration for each channel*/
extern const uint8 NmToCanChnAry[NM_NUMBER_OF_CHANNELS];
extern const uint8 CanToNmChnAry[CANIF_USED_CONTROLLER_NUM];
extern const uint8 nmRxPduIdAry[NM_NUMBER_OF_CHANNELS];
extern const uint8 nmTxPduIdAry[NM_NUMBER_OF_CHANNELS];/*NM TxPDUID in CanIfTxPduConfig*/
extern const nmNetIdType nmNodeChannelID[NM_NUMBER_OF_CHANNELS];/*the node ID for different channel.*/
extern const uint32 nmPduBasicIDAry[NM_NUMBER_OF_CHANNELS];/* NM Basic ID in the network */
extern const uint32 nmPduBasicIDMaskAry[NM_NUMBER_OF_CHANNELS];/* NM Message idbase mask*/
extern const uint32 nmPduSouceIDMaskAry[NM_NUMBER_OF_CHANNELS];/*NM Message source id mask*/

extern const nmTimerCntType nmCountTypAry[NM_NUMBER_OF_CHANNELS];/* Timer: 100ms */
extern const nmTimerCntType nmCountMaxAry[NM_NUMBER_OF_CHANNELS];/* Timer: 260ms */
extern const nmTimerCntType nmCountErrAry[NM_NUMBER_OF_CHANNELS];/* Timer: 1000ms */
extern const nmTimerCntType nmCountWbsAry[NM_NUMBER_OF_CHANNELS];/* Timer: 5000ms */
extern const nmTimerCntType nmCountTxAry [NM_NUMBER_OF_CHANNELS];/* Timer: 30ms */
extern const nmTimerCntType nmCountFastBusoffAry[NM_NUMBER_OF_CHANNELS];/*Timer:50ms*/
extern const nmTimerCntType nmCountSlowBusoffAry[NM_NUMBER_OF_CHANNELS];/*Timer:1000ms*/
#if(T_ACTIVE_MINI_ON == STD_ON)						/*OsekNm_013*/
extern const nmTimerCntType nmCountActMinAry[NM_NUMBER_OF_CHANNELS];/*Timer:4000ms*/
#endif
#if(T_LIMPHOME_MINI_ON == STD_ON)
extern const nmTimerCntType nmCountTLimpHAry[NM_NUMBER_OF_CHANNELS];/*Timer:4000ms*/
#endif
extern const uint8 nmBusOffFastToSlowAry[NM_NUMBER_OF_CHANNELS]; /*Fast Recover times*/
extern const uint16 nmBusOffSlowRecoveryAry[NM_NUMBER_OF_CHANNELS];/*Slow Recover times*/ /*infinite recovery need more than 256*/
extern const nmRxTxCntType  nmRxLimitAry[NM_NUMBER_OF_CHANNELS];/* RX_LIMIT  */
extern const nmRxTxCntType  nmTxLimitAry[NM_NUMBER_OF_CHANNELS];/* TX_LIMIT  */


extern void CanAppMessageDelyCntFun(void);
#endif  /* _OSEKNM_CFG_H_ */
/* End of File */
